package com.punai.standard.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.bos.HeaderBo;
import com.punai.common.utils.DictUtils;
import com.punai.standard.domain.StaSubpkLinkConsume;
import com.punai.standard.domain.StaSubpkLinkConsumeAddress;
import com.punai.standard.domain.bo.StaConsumeBo;
import com.punai.standard.domain.constants.StaConstants;
import com.punai.standard.mapper.StaSubpkLinkConsumeAddressMapper;
import com.punai.standard.mapper.StaSubpkLinkConsumeMapper;
import com.punai.standard.service.StaSubpkLinkConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StaSubpkLinkConsumeServiceImpl extends ServiceImpl<StaSubpkLinkConsumeMapper, StaSubpkLinkConsume> implements StaSubpkLinkConsumeService {

    @Autowired
    private StaSubpkLinkConsumeAddressMapper addressMapper;
    @Autowired
    private StaSubpkLinkConsumeMapper consumeMapper;

    @Override
    public List<HeaderBo> getCategoryHeader(String versionId) {
        List<StaSubpkLinkConsumeAddress> addressList = addressMapper.selectList(
                Wrappers.<StaSubpkLinkConsumeAddress>lambdaQuery()
                        .eq(StaSubpkLinkConsumeAddress::getVersionId, versionId)
        );
        List<HeaderBo> resBos = new ArrayList<>();
        if (CollUtil.isEmpty(addressList)) {
            return resBos;
        }
        for (StaSubpkLinkConsumeAddress staSubpkLinkConsumeAddress : addressList) {
            HeaderBo bo = new HeaderBo();
            String address = staSubpkLinkConsumeAddress.getAddress();
            String feeAddressLabel = DictUtils.getDictLabel("fee_address", address);
            bo.setLabel(feeAddressLabel);
            bo.setProp(StaConstants.REALITY_CONSUME_NAME + address);
            resBos.add(bo);
        }
        List<HeaderBo> rootBos = new ArrayList<>();
        // 添加地区节点
        HeaderBo rootBo = new HeaderBo();
        rootBo.setLabel("地区");
        rootBo.setChildren(resBos);
        rootBos.add(rootBo);
        return rootBos;
    }

    @Override
    public List<StaConsumeBo> listConsume(String linkGxFeatureId,String address, String versionId,Long companyId) {
        return consumeMapper.selectSubpkConsume(linkGxFeatureId,address,versionId,companyId);
    }
}
